package com.loadmore.common.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.loadmore.common.R;
import com.wang.avi.AVLoadingIndicatorView;

/**
 * @author sunleilei
 * @date 2018/7/19
 * @time 9:24
 */
public class StatusLayout extends FrameLayout {
    private RelativeLayout failedLayout;
    private RelativeLayout netWorkLayout;
    private RelativeLayout emptyLayout;
    private AVLoadingIndicatorView loadingIndicatorView;
    public StatusLayout(Context context) {
        this(context,null);
    }

    public StatusLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public StatusLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View content = LayoutInflater.from(context).inflate(R.layout.layout_statu_layout,null);
        loadingIndicatorView = content.findViewById(R.id.loading_view);
        failedLayout = content.findViewById(R.id.failed_layout);
        netWorkLayout = content.findViewById(R.id.network_layout);
        emptyLayout = content.findViewById(R.id.empty_layout);
        addView(content);
        failedLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showLoading();
                if (statusListener != null) {
                    statusListener.retry();
                }
            }
        });

        netWorkLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (statusListener != null) {
                    statusListener.netError();
                }
            }
        });

        emptyLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showLoading();
                if (statusListener != null) {
                    statusListener.retry();
                }
            }
        });
    }

    public void showLoading() {
        setVisibility(VISIBLE);
        failedLayout.setVisibility(GONE);
        netWorkLayout.setVisibility(GONE);
        emptyLayout.setVisibility(GONE);
        loadingIndicatorView.setVisibility(VISIBLE);
    }

    public void showContent() {
        setVisibility(GONE);
    }

    public void showFailed() {

        setVisibility(VISIBLE);
        failedLayout.setVisibility(VISIBLE);
        netWorkLayout.setVisibility(GONE);
        emptyLayout.setVisibility(GONE);
        loadingIndicatorView.setVisibility(GONE);
    }

    public void showNetError() {

        setVisibility(VISIBLE);
        failedLayout.setVisibility(GONE);
        netWorkLayout.setVisibility(VISIBLE);
        emptyLayout.setVisibility(GONE);
        loadingIndicatorView.setVisibility(GONE);
    }
    public void showEmpty() {

        setVisibility(VISIBLE);
        failedLayout.setVisibility(GONE);
        netWorkLayout.setVisibility(GONE);
        emptyLayout.setVisibility(VISIBLE);
        loadingIndicatorView.setVisibility(GONE);
    }


    private StatusListener statusListener;

    public void setStatusListener(StatusListener statusListener) {
        this.statusListener = statusListener;
    }

    public interface StatusListener {
        void retry();
        void netError();
    }
}
